package com.example.shortvideobackend.controller;

import com.example.shortvideobackend.entity.User;
import com.example.shortvideobackend.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/follows")
@CrossOrigin(origins = "*")
public class FollowController {

    @Autowired
    private FollowService followService;

    // 关注用户
    @PostMapping("/follow")
    public ResponseEntity<?> followUser(@RequestParam Long followerId, @RequestParam Long followingId) {
        try {
            followService.followUser(followerId, followingId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "关注成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 取消关注用户
    @PostMapping("/unfollow")
    public ResponseEntity<?> unfollowUser(@RequestParam Long followerId, @RequestParam Long followingId) {
        try {
            followService.unfollowUser(followerId, followingId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "取消关注成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 检查是否已关注
    @GetMapping("/check")
    public ResponseEntity<?> checkFollowStatus(@RequestParam Long followerId, @RequestParam Long followingId) {
        try {
            boolean isFollowing = followService.isFollowing(followerId, followingId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("isFollowing", isFollowing);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取关注列表
    @GetMapping("/following/{userId}")
    public ResponseEntity<?> getFollowing(@PathVariable Long userId) {
        try {
            List<User> following = followService.getFollowing(userId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", following);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取粉丝列表
    @GetMapping("/followers/{userId}")
    public ResponseEntity<?> getFollowers(@PathVariable Long userId) {
        try {
            List<User> followers = followService.getFollowers(userId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", followers);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取关注数量
    @GetMapping("/following-count/{userId}")
    public ResponseEntity<?> getFollowingCount(@PathVariable Long userId) {
        try {
            long count = followService.getFollowingCount(userId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("count", count);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取粉丝数量
    @GetMapping("/followers-count/{userId}")
    public ResponseEntity<?> getFollowersCount(@PathVariable Long userId) {
        try {
            long count = followService.getFollowersCount(userId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("count", count);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 